package com.cmpan.networkdisk.controller;

import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:UserController
 * Package:com.cmpan.networkdisk.controller
 * Description:
 *
 * @Date:2020/7/8 22:51
 * @com.chuangmei
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){

        //session置空
        request.getSession().setAttribute("user",null);

        System.out.println("进入注销controller....");

        return "index";
    }

    @GetMapping("/editor")
    public String editor(HttpServletRequest request, Model model){

        //user加入request
        model.addAttribute("user",request.getSession().getAttribute("user"));

        return "admin/information";
    }

    @GetMapping("/editorinfo")
    public String editorInfor(User user,HttpServletRequest request){

        User u = (User) request.getSession().getAttribute("user");

        if (u != null){
            user.setId(u.getId());
            user.setUsername(null);
            user.setBirthtime(null);
        }

        //service
        Integer count = userService.editorUser(user);

        if (count > 0){
            //重置Session
            request.getSession().setAttribute("user",userService.selectAll());
        }

        return count > 0 ? "index" : "admin/information";
    }

    @GetMapping("/editorPassword")
    public String editorPass(Model model,HttpServletRequest request){

        model.addAttribute("user",request.getSession().getAttribute("user"));

        return "/admin/modify";
    }

}
